<?php

namespace app\api\model\settings;

use app\common\model\settings\Shoptime as ShoptimeModel;
use think\facade\Db;

/**
 * 营业时间模型
 */
class Shoptime extends ShoptimeModel
{
	/**
	 * @Exp    获取区域营业时间
	 * @Author Mr.Z
	 * @Date   2024-03-01
	 * @param  integer    $province  [省id]
	 * @param  integer    $city      [市id]
	 * @param  integer    $ShopSupId [商户id]
	 * @return [type]                [description]
	 */
	public function getAreaTime($province=0,$city=0,$ShopSupId=0)
	{
		if($ShopSupId>0){
			if($detailrs = $this->getdetail(['time_range'=>1,'shop_supplier_id'=>$ShopSupId])){
				return $detailrs;
			}
		}
		if($detailrs = $this->getdetail(['area_id'=>$city])){
			return $detailrs;
		}elseif($detailrs = $this->getdetail(['area_id'=>$province])){
			return $detailrs;
		}else{
			return $this->getdetail(['area_id'=>0]);
		}
	}

	/**
	 * @Exp    获取详细
	 * @Author Mr.Z
	 * @Date   2024-03-01
	 * @param  [Array]     $where [条件]
	 * @return [type]            [description]
	 */
	public function getdetail($where)
	{
		return $this->where($where)->find();
	}

	/**
	 * @Exp    获取营业时间
	 * @Author Mr.Z
	 * @Date   2024-03-01
	 * @param  integer    $province  [省id]
	 * @param  integer    $city      [市id]
	 * @param  integer    $ShopSupId [商户id]
	 * @param  boolean    $getformat     [是否输出格式]
	 * @return [type]               [description]
	 */
	public function getBusinessTime($province=0,$city=0,$ShopSupId=0,$getformat=true){
		$ShopDayTime = $this->getAreaTime($province,$city,$ShopSupId);
		$StartBuildtime = $this->reBuildTime($ShopDayTime['start_time'],false);
		$EndBuildtime = $this->reBuildTime($ShopDayTime['end_time'],$getformat?true:false);
		return [$StartBuildtime,$EndBuildtime];
	}

	/**
	 * @Exp    重组时间
	 * @Author Mr.Z
	 * @Date   2024-03-01
	 * @param  [type]     $stime     [时间]
	 * @param  boolean    $buildname [是否重组前缀]
	 * @return [type]                [description]
	 */
	public function reBuildTime($stime,$buildname=false){
		$ttime = strtotime(date("Y-m-d ".$stime));
		if($buildname){
			$temptimeary = [];
			array_push($temptimeary, ['time'=>strtotime(date("Y-m-d")." 00:00"),'tname'=>'凌晨']);//凌晨
			array_push($temptimeary, ['time'=>strtotime(date("Y-m-d")." 06:00"),'tname'=>'上午']);//上午
			array_push($temptimeary, ['time'=>strtotime(date("Y-m-d")." 15:00"),'tname'=>'下午']);//下午
			array_push($temptimeary, ['time'=>strtotime(date("Y-m-d")." 20:00"),'tname'=>'夜晚']);//夜晚
			array_push($temptimeary, ['time'=>strtotime(date("Y-m-d")." 22:30"),'tname'=>'深夜']);//深夜
			$bfname = "";
			foreach ($temptimeary as $kvalue) {
				if($ttime>=$kvalue['time']){
					$bfname = $kvalue['tname'];
				}
			}
			return $bfname.date("H:i",$ttime);
		}
		return date("H:i",$ttime);
	}

	/**
	 * @Exp    检查营业状况
	 * @Author Mr.Z
	 * @Date   2024-03-01
	 * @param  integer    $province   [省id]
	 * @param  integer    $city       [市id]
	 * @param  integer    $ShopSupId  [商户id]
	 * @return [type]                 [description]
	 */
	public function checkBusiness($province=0,$city=0,$ShopSupId=0){
		$ShopDayTime = $this->getAreaTime($province,$city,$ShopSupId);
		$Start_Time = $ShopDayTime['start_time'];
		$End_Time = $ShopDayTime['end_time'];
		$C_Start_Time = strtotime(date("Y-m-d ".$Start_Time));
		$C_End_Time = strtotime(date("Y-m-d ".$End_Time));
		$C_Temp_Time = time();
		$CheckOpen = $this->checkBusinessTime($C_Start_Time,$C_End_Time,$C_Temp_Time);
		return $CheckOpen;
	}

	/**
	 * @Exp    按时间检查
	 * @Author Mr.Z
	 * @Date   2024-03-01
	 * @param  integer    $starttime [开始时间-时间戳]
	 * @param  integer    $endtime   [结束时间-时间戳]
	 * @param  integer    $checktime [当前时间-时间戳]
	 * @return [type]                [description]
	 */
	public function checkBusinessTime($starttime=0,$endtime=0,$checktime=0){
		if($endtime<$starttime){//次日情况	12-2
			if($checktime>=$starttime){		//12-24
				return true;
			}else{		//0-12
				if($checktime<=$endtime){	//0-2
					return true;
				}	//3-11
			}
		}else{	//6-22
			if($checktime>=$starttime&&$checktime<=$endtime){	//6-22
				return true;
			}	//23-24 0-5
		}
		return false;
	}

}